package com.airS.task.fileMgr.controller.gk;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.common.response.BaseResponse;
import com.airS.task.fileMgr.common.response.ResponseCode;
import com.airS.task.fileMgr.controller.base.SupportController;
import com.airS.task.fileMgr.model.gk.Permission;
import com.airS.task.fileMgr.query.gk.PermissionQuery;
import com.airS.task.fileMgr.security.shiro.realm.SystemAuthorizingRealm;
import com.airS.task.fileMgr.service.gk.PermissionService;
import com.airS.task.fileMgr.utils.shiro.SecurityUtils;
import com.airS.task.fileMgr.utils.url.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 权限管理
 * Created by alan on 2017/3/19.
 */
@Controller
@RequestMapping("/perm")
public class PermissionController extends SupportController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 权限列表
     * @param permissionQuery
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/list")
    public String list(PermissionQuery permissionQuery, HttpServletRequest request, ModelMap modelMap){
        permissionQuery.setRequestUrl(UrlUtils.getLookupPathForRequest(request));
        Page<Permission> permissionPage =  permissionService.queryPermissions(permissionQuery);
        modelMap.put("permissionPage", permissionPage);
        return "admin/modules/gk/permission_list";
    }

    /**
     * 权限删除
     * @param permissionIds
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    public Object remove(@RequestParam("permissionIds[]") List<Integer> permissionIds){
        BaseResponse baseResponse = new BaseResponse();
        SystemAuthorizingRealm.Principal principal = SecurityUtils.getPrincipal();
        // 数据有效性检查
        if(permissionIds == null || permissionIds.isEmpty()){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            return baseResponse;
        }
        // 权限删除
        Integer count = permissionService.delPermissionByIds(permissionIds);
        if(count > 0){
            baseResponse.setMsg("删除成功");
        }else{
            baseResponse.setMsg("删除失败，请重试");
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
        }
        return baseResponse;
    }

    /**
     * 权限新增页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(ModelMap modelMap){
        modelMap.put("permission", new Permission());
        List<Permission> allPermission = permissionService.getAllPermissionToList();
        modelMap.put("allPermission", allPermission);
        return "admin/modules/gk/permission_add";
    }

    /**
     * 权限新增
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public String add(Permission permission, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        SystemAuthorizingRealm.Principal principal = SecurityUtils.getPrincipal();
        session.removeAttribute("permBaseResponse");
        // 权限新增
        permission.setUpdatePeople(null);
        permission.setUpdateTime(null);
        permission.setCreateTime(new Date());
        permission.setCreatePeople(principal.getEmployeeName());
        Integer permissionId = permissionService.addPermission(permission);
        // 封装业务操作状态
        if(permissionId > 0){
            baseResponse.setMsg("操作成功");
        }else{
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
            baseResponse.setMsg("操作失败，请重试");
        }
        session.setAttribute("permBaseResponse", baseResponse);
        return "redirect:/perm/list?pclass=p_6&currentClass=c_8";
    }

    /**
     * 权限修改页面
     * @param modelMap
     * @param query
     * @param session
     * @return
     */
    @RequestMapping("/toModify")
    public String toModify(ModelMap modelMap, PermissionQuery query, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        session.removeAttribute("permBaseResponse");
        // 数据有效性检查
        if(query.getPermissionId() == null){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            session.setAttribute("permBaseResponse", baseResponse);
            return "redirect:/perm/list?pclass=p_6&currentClass=c_8";
        }
        // 封装页面模型
        Permission permission = permissionService.queryPermissionById(query.getPermissionId());
        if(permission.getPermissionId() == null || permission.getPermissionId() <= 0){
            baseResponse.setCode(ResponseCode.NOT_EXIST_FAIL);
            baseResponse.setMsg("查找不到当前权限，请重试");
            session.setAttribute("permBaseResponse", baseResponse);
            return "redirect:/perm/list?pclass=p_6&currentClass=c_8";
        }
        List<Permission> allPermission = permissionService.getAllPermissionToList();
        for(Iterator<Permission> it = allPermission.iterator(); it.hasNext();){
            if(it.next().getPermissionId() == query.getPermissionId()){
                it.remove();
            }
        }
        modelMap.put("allPermission", allPermission);
        modelMap.put("permission", permission);
        return "admin/modules/gk/permission_modify";
    }

    /**
     * 权限修改
     * @param permission
     * @param modelMap
     * @param session
     * @return
     */
    @RequestMapping("/modify")
    public String modify(Permission permission, ModelMap modelMap, HttpSession session){
        BaseResponse baseResponse = new BaseResponse();
        SystemAuthorizingRealm.Principal principal = SecurityUtils.getPrincipal();
        session.removeAttribute("permBaseResponse");
        // 数据有效性检查
        if(permission.getPermissionId() == null || permission.getParentId() == null || permission.getPermissionId() == permission.getParentId()){
            baseResponse.setCode(ResponseCode.PARAM_FAIL);
            baseResponse.setMsg("参数异常，请重试");
            session.setAttribute("permBaseResponse", baseResponse);
            return "redirect:/perm/list?pclass=p_6&currentClass=c_8";
        }
        // 权限修改
        permission.setUpdatePeople(principal.getEmployeeName());
        permission.setUpdateTime(new Date());
        Integer count = permissionService.updatePermissionById(permission);
        // 封装业务操作状态
        if(count > 0){
            baseResponse.setMsg("操作成功");
        }else{
            baseResponse.setCode(ResponseCode.BUSINESS_FAIL);
            baseResponse.setMsg("操作失败，请重试");
        }
        session.setAttribute("permBaseResponse", baseResponse);
        return "redirect:/perm/list?pclass=p_6&currentClass=c_8";
    }

    /**
     * 获取操作结果
     * @param session
     * @return
     */
    @RequestMapping("/getOperaStatus")
    @ResponseBody
    public Object getOperaStatus( HttpSession session){
        Object status = session.getAttribute("permBaseResponse");
        session.removeAttribute("permBaseResponse");
        return status;
    }

}
